package com.example.shop_2304.login

import androidx.lifecycle.viewModelScope
import com.example.base.entity.CodeSendEntity
import com.example.base.entity.LoginEntity
import com.example.base.entity.Res
import com.example.base.entity.UploadEntity
import com.example.base.entity.UserInfoEntity
import com.example.base.mvvm.BaseViewModel
import com.example.base.utils.SingleLiveData
import com.google.gson.Gson
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.http.Query

class LoginViewModel:BaseViewModel() {
    //获取验证码
    val codeSendSuccess = SingleLiveData<Res<CodeSendEntity>>()
    val codeSendFail = SingleLiveData<String>()
    fun codeSend(@Query("phone")phone:String){
        viewModelScope.launch {
            try {
                codeSendSuccess.postValue(apiService.codeSend(phone))
            }catch (e:Exception){
                codeSendFail.postValue(e.message.toString())
            }
        }
    }
    //验证码登录
    val loginCodeSuccess = SingleLiveData<Res<LoginEntity>>()
    val loginCodeFail = SingleLiveData<String>()
    fun loginCode(captcha:String,phone:String){
        viewModelScope.launch {
            try {
               val map = mutableMapOf<String,Any>()
               map["captcha"]  = captcha
               map["phone"]  = phone
                loginCodeSuccess.postValue(apiService.loginCode(mapToBody(map)))
            }catch (e:Exception){
                loginCodeFail.postValue(e.message.toString())
            }
        }
    }
    //获取用户信息
    val userInfoSuccess = SingleLiveData<Res<UserInfoEntity>>()
    val userInfoFail = SingleLiveData<String>()
    fun userInfo(){
        viewModelScope.launch {
            try {
                userInfoSuccess.postValue(apiService.userInfo())
            } catch (e: Exception) {
                userInfoFail.postValue(e.message.toString())
            }
        }
    }
    //修改密码
    val updatePwdSuccess = SingleLiveData<Res<Boolean>>()
    val updatePwdFail = SingleLiveData<String>()
    fun updatePwd(account:String,captcha:String,password:String){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["account"] = account
                map["captcha"] = captcha
                map["password"] = password
                updatePwdSuccess.postValue(apiService.updatePwd(mapToBody(map)))
            } catch (e: Exception) {
                updatePwdFail.postValue(e.message.toString())
            }

        }
    }
    //图片上传
    val uploadSuccess = SingleLiveData<Res<UploadEntity>>()
    val uploadFail = SingleLiveData<String>()
    fun upload(model: String,part: MultipartBody.Part,pid:String){
        viewModelScope.launch {
            try {
                uploadSuccess.postValue(apiService.upload(model, part, pid))
            } catch (e: Exception) {
                uploadFail.postValue(e.message.toString())
            }
        }
    }
}